package com.cek.tdrive.travel;

import java.util.List;

import com.cek.tdrive.adapter.VehicleAdapter;
import com.cek.tdrive.base.TDriveBaseActivity;
import com.cek.tdrive.comon.HttpASync;
import com.cek.tdrive.comon.HttpASync.OnRequest;
import com.cek.tdrive.constant.AppConstant;
import com.cek.tdrive.data.Vehicle;
import com.cek.tdrive.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class CostActivity extends TDriveBaseActivity implements OnRequest, OnItemSelectedListener, OnClickListener {
	private Spinner vehicles;
	private Context ctx;
	private float petrolPrice; 
	private float dieselPrice;
	private TextView cost;
	private List<Vehicle> vehicleListData;
	private long distance;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.cost);
		ctx = this;
		vehicles = (Spinner) findViewById(R.id.spinner1);
		vehicles.setOnItemSelectedListener(this);
		distance  = getIntent().getLongExtra("DISTANCE", 10)/1000;
	    System.out.println(" distance ## "+distance);
	
		cost=(TextView)findViewById(R.id.textView4);
		
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME, 0);
		String uName = pref.getString("username", null);
		HttpASync async = new HttpASync();
		String url = AppConstant.ADD_VEHICLE + "?emailid=" + uName
				+ "&method=get";
		async.setOnRequestListener(this);
		async.execute(url);
		showProgress("Loading your Vehicles ... ");
	}

	@Override
	public void onResponseReceived(String response) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				hideProgress();
				
			}
		});
		if (response.contains("#")) {
			System.out.println("response = "+response);
			String fuelPrices[] = response.split("#");
			System.out.println("fuelPrice : "+fuelPrices[0]+","+fuelPrices[1]);
			 petrolPrice = Float.parseFloat(fuelPrices[0]);
			 dieselPrice = Float.parseFloat(fuelPrices[1]);
			

		} else {
			Gson gson = new Gson();
			int i = 0;
			vehicleListData = gson.fromJson(response,
					new TypeToken<List<Vehicle>>() {
					}.getType());
			String[] array_spinner = new String[vehicleListData.size()];
			for (Vehicle vehicle : vehicleListData) {
				System.out.println("Vehicle : " + vehicle.getVehicleName());
				array_spinner[i++] = vehicle.getVehicleName();

			}
			final String[] temp = array_spinner;
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					ArrayAdapter adapter = new ArrayAdapter(ctx,
							android.R.layout.simple_spinner_item, temp);
					vehicles.setAdapter(adapter);
				}
			});
			
			SharedPreferences pref = getSharedPreferences(AppConstant.PREFS_NAME, 0);
			String uName = pref.getString("username", null);
			HttpASync async = new HttpASync();
			String url = AppConstant.COST;
			async.setOnRequestListener(this);
			async.execute(url);
		}

	}

	@Override
	public void onClick(View v) {
		if (v.getId()==R.id.button1){
			int selPos = vehicles.getSelectedItemPosition();
			vehicleListData.get(selPos);
		}
		
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View v, int position,
			long id) {
	String type=vehicleListData.get(position).getType();
	float mileage=vehicleListData.get(position).getMileage();
	float cost=0;
	if ( type.contains("Petrol")) {
		//cost=petrolPrice*mileage;
		cost = petrolPrice* (distance/mileage);
	}	else {
		cost = dieselPrice* (distance/mileage);
	}
		System.out.println("Cost="+cost);
		this.cost.setText("Rs. "+cost + "/-");
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

}
